package com.android.KickFlybug.GameObject;

import java.util.Random;

import com.android.KickFlybug.Until.ActivityUtil;
import com.android.KickFlybug.Until.AudioUtil;
import com.android.KickFlybug.Until.BitmapManager;
import com.android.KickFlybug.Until.GameObjData;
import com.android.R;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;

public class MainLayer extends Layer
{

	private static final int LIVE_FLY_COUNT = 50; // 屏幕中最多存活的蚊子数量

	static final Random rdm = new Random();
	
	public static final int TYPE_MOS = 1;//蚊子
	public static final int TYPE_FLY = 2;//苍蝇
	public static final int TYPE_COCKROACH = 3;//蟑螂
	private Bitmap bmpMos = BitmapManager.getBitmap(R.drawable.mos);//蚊子
	private Bitmap bmpMosDead = BitmapManager.getBitmap(R.drawable.mosdead);//死蚊子
	private Bitmap bmpFly = BitmapManager.getBitmap(R.drawable.fly);//苍蝇
	private Bitmap bmpFlyDead = BitmapManager.getBitmap(R.drawable.flydead);//死苍蝇
	private Bitmap bmpCockroach = BitmapManager.getBitmap(R.drawable.cockroach);//蟑螂COCKROACH
	private Bitmap bmpCockroachDead = BitmapManager.getBitmap(R.drawable.cockroachdead);//死蟑螂COCKROACH
	private Bitmap mbmpPest;//害虫
	private Bitmap mbmpPestDead;//死害虫
	
	
	//每次生成蚊子的时间(毫秒)
	private int createMosTime = 50;
	private int count;//计数器
	private long lastUpdate;

	private Paint p = new Paint();

	public MainLayer(int x, int y, int w, int h)
	{
		super(x, y, w, h);
		init();
	}

	@Override
	public void init()
	{
		//生成蚊子
	//	for (int i = 0; i < LIVE_FLY_COUNT; i++)
		{
		//	addGameObj(new Fly());
		}
		p.setColor(Color.BLACK);
		p.setAntiAlias(true);
		p.setTextSize(25);
		p.setAlpha(50);
	}

	@Override
	public void logic()
	{
		
		if(count%createMosTime==0)
		{
			//加载随机的害虫
			int Type = rdm.nextInt(3)+1;
			switch (Type) {
			case TYPE_MOS:
				mbmpPest = bmpMos;
				mbmpPestDead = bmpMosDead;
				break;
			case TYPE_FLY:
				mbmpPest = bmpFly;
				mbmpPestDead = bmpFlyDead;				
				break;	
			case TYPE_COCKROACH:
				mbmpPest = bmpCockroach;
				mbmpPestDead = bmpCockroachDead;				
				break;	
			default:
				break;
			}
			addGameObj(new Fly(mbmpPest,mbmpPestDead,Type));
		}
		count++;
		synchronized (objs)
		{
			for (BaseGameObj obj : objs)
			{
				obj.logic();
				for (BaseGameObj obj2 : objs)
				{
					Fly f = (Fly) obj;
					if (!f.equals(obj2) && !f.dead)
					{
						f.collisionTo((Fly) obj2);
					}
				}
			}
		}
		//如果当前时间与上次更新时间超过1秒那么累加
		long now = System.currentTimeMillis();
		if (now - lastUpdate >= 1000)
		{
			GameObjData.CURRENT_USE_TIME++;
			lastUpdate = now;
		}
	}
	//清扫死害虫
	public void clean()
	{
		synchronized (objs)
		{
			for (BaseGameObj obj : objs)
			{
				obj.logic();
				for (BaseGameObj obj2 : objs)
				{
					Fly f = (Fly) obj;
					if (f.dead)
					{
						f.clean = true;
					}
				}
			}
		}
		//重置是否清扫状态
		GameObjData.ISCLEAN = false;
	
	}

	@Override
	public void paint(Canvas c)
	{
		super.paint(c);
		c.drawText("得分: " + GameObjData.CURRENT_SCORE, 0, 25, p);
		c.drawText("用时: " + GameObjData.CURRENT_USE_TIME, 0, 60, p);
	}

	public void onTouch(MotionEvent event)
	{
		int x = (int) event.getX();
		int y = (int) event.getY();

		// 播放声音
		AudioUtil.PlayMusic(ActivityUtil.myContext, R.raw.musickill);
		//ActivityUtil.PlaySoundPool(ActivityUtil.myContext,  R.raw.music3);
		// 震动一下
		// ActivityUtil.getVibrator(ActivityUtil.myContext);
		synchronized (objs)
		{
			for (BaseGameObj obj : objs)
			{
				Fly f = (Fly) obj;
				if (!f.dead && f.contains(x, y))
				{
					f.dead = true;
					switch (f.mFlyType) {
					case 1://蚊子
						GameObjData.CURRENT_SCORE += 10;
						break;
					case 2://苍蝇
						GameObjData.CURRENT_SCORE += 20;
						break;
					case 3://蟑螂
						GameObjData.CURRENT_SCORE += 50;
						break;					
					default:
						break;
					}
					return;
				}
			}
		}
	}
	
}
